package com.heima.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
  @Override
  public ResponseResult findByNameAndPage(ChannelDTO dto) {
    // 1.对参数进行分析
    dto.checkParam();
    // 2.查询用户列表,先根据name 进行模糊查询， 然后根据排序号，排序
    IPage<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
    // 进行条件查询，如果有name，就进行模糊查询
    LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
    if (StringUtils.isNotBlank(dto.getName())) {
      wrapper.like(AdChannel::getName, dto.getName());
    }
    wrapper.orderByAsc(AdChannel::getOrd);
    IPage<AdChannel> iPage = this.page(page, wrapper);
    // 3.反应结果的封装

    ResponseResult responseResult =
        new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal());
    responseResult.setData(iPage.getRecords());

    return responseResult;
  }

  @Override
  public ResponseResult insert(AdChannel channel) {
    // 1.进行参数的校验，是否为空，长度是否大于10
    if (StringUtils.isBlank(channel.getName()) && channel.getName().length() > 10) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空，且长度要小于10");
    }
    // 从数据库查询数据，是否频道名称相同
    LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
    wrapper.eq(AdChannel::getName, channel.getName());
    int count = this.count(wrapper);
    if (count > 0) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称重复");
    }
    // 新增
    channel.setCreatedTime(new Date());
    this.save(channel);
    // 返回正确的结果
    return ResponseResult.okResult();
  }

  /**
   * 修改频道及频道状态
   * @param adChannel
   * @return
   */
  @Override
  public ResponseResult update(AdChannel adChannel) {
    //1 参数校验
    if (adChannel == null || adChannel.getId() == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    //2 执行修改
    AdChannel channel = getById(adChannel.getId());
    if (channel == null) {
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
    }
    //3. 校验名称唯一性
    if(StringUtils.isNotBlank(adChannel.getName())
            &&
            !adChannel.getName().equals(channel.getName())){
      int count = this.count(Wrappers.<AdChannel>lambdaQuery()
              .eq(AdChannel::getName, adChannel.getName()));
      if(count > 0){
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
      }
    }
    updateById(adChannel);
    //4 返回结果
    return ResponseResult.okResult();
  }

  @Override
  public ResponseResult deleteById(Integer id) {
    //1.检查参数
    if(id == null){
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    //2.判断当前频道是否存在 和 是否有效
    AdChannel adChannel = getById(id);
    if(adChannel==null){
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    // 启用状态下不能删除
    if (adChannel.getStatus()) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }
    //3.删除频道
    removeById(id);
    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
  }
}
